package com.example.mylibrarybase.yumengmeng14.utils;

import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.utils.LogUtils;

import java.util.List;

/**
 * Created by 35876 于萌萌
 * 创建日期: 12:48 . 2016年10月24日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public class buildClass {
    public static final int FIRST = 0;
    public static final int LAST = 1;
    buildClass mBuildClass;
        public Context mContext;
    private LinearLayout mLinearLayout;
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    private LayoutTransition mTransitioner;

    public buildClass(Context context) {
        this.mContext =context;

    }

    public buildClass setLinearLayout(LinearLayout linearLayout){
            mLinearLayout = linearLayout;
            return  this;
        }
        public buildClass addLast(String textViewName){
           layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (null==mContext){
                LogUtils.i("动态生成没有上下文!");
                return this;
            }

            addlinertext(textViewName, layoutParams,LAST);
            return this;
        }

   

    public buildClass addfirst(String textViewName){
          layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (null==mContext){
                LogUtils.i("动态生成没有上下文!");
                return this;
            }

        addlinertext(textViewName, layoutParams, FIRST);
            return this;
        }
    
        public buildClass setTextViewList(List<String> textViewList){
            TextView textView ;
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            for (int i = 0; i < textViewList.size(); i++) {
                String t = textViewList.get(i);
                textView =  new TextView(mContext);
                textView.setText(t);
                textView.setLayoutParams(layoutParams);
                mLinearLayout.addView(textView);
            }
            return this;
        }
        public buildClass rMoveLast(){
            rMove(LAST);
            return this;
        }

    private void rMove(int mode) {
        int childCount = mLinearLayout.getChildCount();
        LogUtils.i("里面有多少"+childCount);
        if (childCount>0){
            if (mode==LAST){
                mLinearLayout.removeViewAt(childCount-1);
            }else{
                mLinearLayout.removeViewAt(0);
            }
        }else{
            LogUtils.i("里面没有东西");
        }
    }

    public buildClass rMovefrist(){
        rMove(FIRST);
            return this;
        }
    private void addlinertext(String textViewName, LinearLayout.LayoutParams layoutParams, int mode) {
        TextView textView = new TextView(mContext);
        textView.setPadding(20, 20, 20, 20);
        textView.setBackground(mContext.getResources().getDrawable(R.color.red));
        textView.setText(textViewName);
        textView.setLayoutParams(layoutParams);
        mTransitioner = new LayoutTransition();
        //入场动画:view在这个容器中消失时触发的动画
        ObjectAnimator animIn = ObjectAnimator.ofFloat(null, "rotationY", 0f, 360f,0f);
        mTransitioner.setAnimator(LayoutTransition.APPEARING, animIn);

        //出场动画:view显示时的动画
        ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "rotation", 0f, 90f, 0f);
        mTransitioner.setAnimator(LayoutTransition.DISAPPEARING, animOut);

        mLinearLayout.setLayoutTransition(mTransitioner);
        if (mode == FIRST){
            mLinearLayout.addView(textView,0);
        }else{
            mLinearLayout.addView(textView);
        }

    }
    }


